package c.x.jy.api.controller;

import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import c.x.jy.common.annotation.SysLog;
import c.x.jy.common.utils.Constant;
import c.x.jy.common.utils.R;
import c.x.jy.service.redis.Cache;
import c.x.jy.service.service.SysUserService;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/api/sysuser")
public class SysUserController extends AbstractController {

	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private Cache redisUtils;

	/**
	 * 重置密码
	 * 
	 * @param userId
	 * @return
	 */
	@SysLog(" 用户修改密码")
	@RequestMapping(value = "/sys/updatepassword", method = RequestMethod.PUT)
	@ResponseBody
	public R updatepassword(String oldPassWord, String newPassWord) {

		sysUserService.SetPassword(getUserId(), oldPassWord, newPassWord);
		Set<String> keys = redisUtils.keys(Constant.USERKEY + getUserId() + ":*");
		if (keys != null)
			keys.forEach(k -> {
				if (k != null)
					redisUtils.remove(redisUtils.get(k));
			});

		return R.ok();
	}

}
